/*
 * FileSystem.cc
 *
 *  Created on: 11 mrt. 2011
 *      Author: vincentvanandel, Jonathan Eenkhoorn, Sander Schulenburg
 */

#include "FileSystem.h"

FileSystem::FileSystem( const char* device )
{
	soft = new list<OpenFile*>();
}

Inode*		FileSystem::findInode( const char * absolute_path )
{
	return 0;
}

Inode* 		FileSystem::findFile( Inode * dirptr, const char * filename )
{
	return 0;
}

void		FileSystem::releaseInode( Inode * ip )
{
}

OpenFile* 	FileSystem::open( const char * path )
{
	OpenFile* file = new OpenFile();
  	file->set(fopen(path, "r+"));
	soft->push_back(file);
	return file;
}

int		FileSystem::write( OpenFile * fileptr, char buffer[], int count )
{
	FILE * temp = fileptr->get();
	int teller = 0;
	if(temp!=NULL)
	{
		while(teller<count)
		{
			fputc(buffer[teller], temp);
			teller++;
		}
		fileptr->setIopos(ftell(temp));
	}
	return teller;
}

int		FileSystem::read( OpenFile * fileptr, char buffer[], int count )
{
	FILE * temp = fileptr->get();
	int teller = 0;
	char c;
	if(temp!=NULL)
	{
		while(!feof(temp)&&teller<count)
		{
			c = fgetc(temp);
			if(c==EOF)
			{
				break;
			}
			else
			{
				buffer[teller] = c;
			}
			teller++;
		}

		fileptr->setIopos(ftell(fileptr->get()));
		printf("%s", buffer);
	}
	return teller;
}

//Whence, opties: SEEK_SET, SEEK_CUR, SEEK_END

int		FileSystem::lseek( OpenFile * fileptr, long offset, int whence )
{
	fileptr->setIopos(fseek(fileptr->get(), offset, whence));
	return fileptr->getIopos();
	
}

void		FileSystem::close( OpenFile * fileptr )
{
	fclose(fileptr->get());
	fileptr->set(NULL);
}
